package com.atguigu.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.springcloud.myhandler.CustomerBlockHandler;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lenovo
 * @version 1.0
 * @date 2021/1/15 10:50
 */
@RestController
public class RateLimitController {

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource", blockHandler = "handleException")
    public String byResource(){
        return HttpStatus.OK + " 按资源名称测试OK ";
    }

    public String handleException(BlockException e){
        return HttpStatus.BAD_REQUEST + e.getClass().getCanonicalName() + " 被限流了把，服务不可用了把！！！";
    }


    @GetMapping("/rateLimit/byUrl")
    @SentinelResource(value = "byUrl")
    public String byUrl(){
        return HttpStatus.OK + " 按url限流测试OK ";
    }


    @GetMapping("/rateLimit/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler", blockHandlerClass = CustomerBlockHandler.class , blockHandler = "handlerException2")
    public String customerBlockHandler(){
        return HttpStatus.OK + " customerBlockHandler限流测试OK ";
    }


}
